<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta charset="utf-8" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="generator" content="pandoc" />
<meta http-equiv="X-UA-Compatible" content="IE=EDGE" />




<title>Molecular clock</title>

<script src="site_libs/jquery-1.11.3/jquery.min.js"></script>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link href="site_libs/bootstrap-3.3.5/css/cosmo.min.css" rel="stylesheet" />
<script src="site_libs/bootstrap-3.3.5/js/bootstrap.min.js"></script>
<script src="site_libs/bootstrap-3.3.5/shim/html5shiv.min.js"></script>
<script src="site_libs/bootstrap-3.3.5/shim/respond.min.js"></script>
<script src="site_libs/navigation-1.1/tabsets.js"></script>
<link href="site_libs/highlightjs-9.12.0/textmate.css" rel="stylesheet" />
<script src="site_libs/highlightjs-9.12.0/highlight.js"></script>
<link href="site_libs/font-awesome-5.1.0/css/all.css" rel="stylesheet" />
<link href="site_libs/font-awesome-5.1.0/css/v4-shims.css" rel="stylesheet" />

<style type="text/css">code{white-space: pre;}</style>
<style type="text/css">
  pre:not([class]) {
    background-color: white;
  }
</style>
<script type="text/javascript">
if (window.hljs) {
  hljs.configure({languages: []});
  hljs.initHighlightingOnLoad();
  if (document.readyState && document.readyState === "complete") {
    window.setTimeout(function() { hljs.initHighlighting(); }, 0);
  }
}
</script>



<style type="text/css">
h1 {
  font-size: 34px;
}
h1.title {
  font-size: 38px;
}
h2 {
  font-size: 30px;
}
h3 {
  font-size: 24px;
}
h4 {
  font-size: 18px;
}
h5 {
  font-size: 16px;
}
h6 {
  font-size: 12px;
}
.table th:not([align]) {
  text-align: left;
}
</style>




<style type = "text/css">
.main-container {
  max-width: 940px;
  margin-left: auto;
  margin-right: auto;
}
code {
  color: inherit;
  background-color: rgba(0, 0, 0, 0.04);
}
img {
  max-width:100%;
}
.tabbed-pane {
  padding-top: 12px;
}
.html-widget {
  margin-bottom: 20px;
}
button.code-folding-btn:focus {
  outline: none;
}
summary {
  display: list-item;
}
</style>


<style type="text/css">
/* padding for bootstrap navbar */
body {
  padding-top: 51px;
  padding-bottom: 40px;
}
/* offset scroll position for anchor links (for fixed navbar)  */
.section h1 {
  padding-top: 56px;
  margin-top: -56px;
}
.section h2 {
  padding-top: 56px;
  margin-top: -56px;
}
.section h3 {
  padding-top: 56px;
  margin-top: -56px;
}
.section h4 {
  padding-top: 56px;
  margin-top: -56px;
}
.section h5 {
  padding-top: 56px;
  margin-top: -56px;
}
.section h6 {
  padding-top: 56px;
  margin-top: -56px;
}
.dropdown-submenu {
  position: relative;
}
.dropdown-submenu>.dropdown-menu {
  top: 0;
  left: 100%;
  margin-top: -6px;
  margin-left: -1px;
  border-radius: 0 6px 6px 6px;
}
.dropdown-submenu:hover>.dropdown-menu {
  display: block;
}
.dropdown-submenu>a:after {
  display: block;
  content: " ";
  float: right;
  width: 0;
  height: 0;
  border-color: transparent;
  border-style: solid;
  border-width: 5px 0 5px 5px;
  border-left-color: #cccccc;
  margin-top: 5px;
  margin-right: -10px;
}
.dropdown-submenu:hover>a:after {
  border-left-color: #ffffff;
}
.dropdown-submenu.pull-left {
  float: none;
}
.dropdown-submenu.pull-left>.dropdown-menu {
  left: -100%;
  margin-left: 10px;
  border-radius: 6px 0 6px 6px;
}
</style>

<script>
// manage active state of menu based on current page
$(document).ready(function () {
  // active menu anchor
  href = window.location.pathname
  href = href.substr(href.lastIndexOf('/') + 1)
  if (href === "")
    href = "index.html";
  var menuAnchor = $('a[href="' + href + '"]');

  // mark it active
  menuAnchor.parent().addClass('active');

  // if it's got a parent navbar menu mark it active as well
  menuAnchor.closest('li.dropdown').addClass('active');
});
</script>

<!-- tabsets -->

<style type="text/css">
.tabset-dropdown > .nav-tabs {
  display: inline-table;
  max-height: 500px;
  min-height: 44px;
  overflow-y: auto;
  background: white;
  border: 1px solid #ddd;
  border-radius: 4px;
}

.tabset-dropdown > .nav-tabs > li.active:before {
  content: "";
  font-family: 'Glyphicons Halflings';
  display: inline-block;
  padding: 10px;
  border-right: 1px solid #ddd;
}

.tabset-dropdown > .nav-tabs.nav-tabs-open > li.active:before {
  content: "&#xe258;";
  border: none;
}

.tabset-dropdown > .nav-tabs.nav-tabs-open:before {
  content: "";
  font-family: 'Glyphicons Halflings';
  display: inline-block;
  padding: 10px;
  border-right: 1px solid #ddd;
}

.tabset-dropdown > .nav-tabs > li.active {
  display: block;
}

.tabset-dropdown > .nav-tabs > li > a,
.tabset-dropdown > .nav-tabs > li > a:focus,
.tabset-dropdown > .nav-tabs > li > a:hover {
  border: none;
  display: inline-block;
  border-radius: 4px;
}

.tabset-dropdown > .nav-tabs.nav-tabs-open > li {
  display: block;
  float: none;
}

.tabset-dropdown > .nav-tabs > li {
  display: none;
}
</style>

<!-- code folding -->




</head>

<body>


<div class="container-fluid main-container">




<div class="navbar navbar-default  navbar-fixed-top" role="navigation">
  <div class="container">
    <div class="navbar-header">
      <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar">
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
      <a class="navbar-brand" href="index.html">ipmn-pdac-timing</a>
    </div>
    <div id="navbar" class="navbar-collapse collapse">
      <ul class="nav navbar-nav">
        <li>
  <a href="index.html">Home</a>
</li>
<li>
  <a href="about.html">About</a>
</li>
<li>
  <a href="license.html">License</a>
</li>
      </ul>
      <ul class="nav navbar-nav navbar-right">
        <li>
  <a href="https://gitlab.com/rscharpf/ipmn-timing">
    <span class="fa fa-gitlab"></span>
     
    Source code
  </a>
</li>
      </ul>
    </div><!--/.nav-collapse -->
  </div><!--/.container -->
</div><!--/.navbar -->

<!-- Add a small amount of space between sections. -->
<style type="text/css">
div.section {
  padding-top: 12px;
}
</style>

<div class="fluid-row" id="header">



<h1 class="title toc-ignore">Molecular clock</h1>

</div>


<p>
<button type="button" class="btn btn-default btn-workflowr btn-workflowr-report" data-toggle="collapse" data-target="#workflowr-report">
<span class="glyphicon glyphicon-list" aria-hidden="true"></span> workflowr <span class="glyphicon glyphicon-ok text-success" aria-hidden="true"></span>
</button>
</p>
<div id="workflowr-report" class="collapse">
<ul class="nav nav-tabs">
<li class="active">
<a data-toggle="tab" href="#summary">Summary</a>
</li>
<li>
<a data-toggle="tab" href="#checks"> Checks <span class="glyphicon glyphicon-ok text-success" aria-hidden="true"></span> </a>
</li>
<li>
<a data-toggle="tab" href="#versions">Past versions</a>
</li>
</ul>
<div class="tab-content">
<div id="summary" class="tab-pane fade in active">
<p>
<strong>Last updated:</strong> 2019-08-28
</p>
<p>
<strong>Checks:</strong> <span class="glyphicon glyphicon-ok text-success" aria-hidden="true"></span> 7 <span class="glyphicon glyphicon-exclamation-sign text-danger" aria-hidden="true"></span> 0
</p>
<p>
<strong>Knit directory:</strong> <code>ipmn-timing/</code> <span class="glyphicon glyphicon-question-sign" aria-hidden="true" title="This is the local directory in which the code in this file was executed."> </span>
</p>
<p>
This reproducible <a href="http://rmarkdown.rstudio.com">R Markdown</a> analysis was created with <a
  href="https://github.com/jdblischak/workflowr">workflowr</a> (version 1.4.0.9000). The <em>Checks</em> tab describes the reproducibility checks that were applied when the results were created. The <em>Past versions</em> tab lists the development history.
</p>
<hr>
</div>
<div id="checks" class="tab-pane fade">
<div id="workflowr-checks" class="panel-group">
<div class="panel panel-default">
<div class="panel-heading">
<p class="panel-title">
<a data-toggle="collapse" data-parent="#workflowr-checks" href="#strongRMarkdownfilestronguptodate"> <span class="glyphicon glyphicon-ok text-success" aria-hidden="true"></span> <strong>R Markdown file:</strong> up-to-date </a>
</p>
</div>
<div id="strongRMarkdownfilestronguptodate" class="panel-collapse collapse">
<div class="panel-body">
<p>Great! Since the R Markdown file has been committed to the Git repository, you know the exact version of the code that produced these results.</p>
</div>
</div>
</div>
<div class="panel panel-default">
<div class="panel-heading">
<p class="panel-title">
<a data-toggle="collapse" data-parent="#workflowr-checks" href="#strongEnvironmentstrongempty"> <span class="glyphicon glyphicon-ok text-success" aria-hidden="true"></span> <strong>Environment:</strong> empty </a>
</p>
</div>
<div id="strongEnvironmentstrongempty" class="panel-collapse collapse">
<div class="panel-body">
<p>Great job! The global environment was empty. Objects defined in the global environment can affect the analysis in your R Markdown file in unknown ways. For reproduciblity it’s best to always run the code in an empty environment.</p>
</div>
</div>
</div>
<div class="panel panel-default">
<div class="panel-heading">
<p class="panel-title">
<a data-toggle="collapse" data-parent="#workflowr-checks" href="#strongSeedstrongcodesetseed20190724code"> <span class="glyphicon glyphicon-ok text-success" aria-hidden="true"></span> <strong>Seed:</strong> <code>set.seed(20190724)</code> </a>
</p>
</div>
<div id="strongSeedstrongcodesetseed20190724code" class="panel-collapse collapse">
<div class="panel-body">
<p>The command <code>set.seed(20190724)</code> was run prior to running the code in the R Markdown file. Setting a seed ensures that any results that rely on randomness, e.g. subsampling or permutations, are reproducible.</p>
</div>
</div>
</div>
<div class="panel panel-default">
<div class="panel-heading">
<p class="panel-title">
<a data-toggle="collapse" data-parent="#workflowr-checks" href="#strongSessioninformationstrongrecorded"> <span class="glyphicon glyphicon-ok text-success" aria-hidden="true"></span> <strong>Session information:</strong> recorded </a>
</p>
</div>
<div id="strongSessioninformationstrongrecorded" class="panel-collapse collapse">
<div class="panel-body">
<p>Great job! Recording the operating system, R version, and package versions is critical for reproducibility.</p>
</div>
</div>
</div>
<div class="panel panel-default">
<div class="panel-heading">
<p class="panel-title">
<a data-toggle="collapse" data-parent="#workflowr-checks" href="#strongCachestrongnone"> <span class="glyphicon glyphicon-ok text-success" aria-hidden="true"></span> <strong>Cache:</strong> none </a>
</p>
</div>
<div id="strongCachestrongnone" class="panel-collapse collapse">
<div class="panel-body">
<p>Nice! There were no cached chunks for this analysis, so you can be confident that you successfully produced the results during this run.</p>
</div>
</div>
</div>
<div class="panel panel-default">
<div class="panel-heading">
<p class="panel-title">
<a data-toggle="collapse" data-parent="#workflowr-checks" href="#strongFilepathsstrongrelative"> <span class="glyphicon glyphicon-ok text-success" aria-hidden="true"></span> <strong>File paths:</strong> relative </a>
</p>
</div>
<div id="strongFilepathsstrongrelative" class="panel-collapse collapse">
<div class="panel-body">
<p>Great job! Using relative paths to the files within your workflowr project makes it easier to run your code on other machines.</p>
</div>
</div>
</div>
<div class="panel panel-default">
<div class="panel-heading">
<p class="panel-title">
<a data-toggle="collapse" data-parent="#workflowr-checks" href="#strongRepositoryversionstrongahrefhttpsgitlabcomrscharpfipmntimingtreea32dbed797e83eb45f85a7ac2bad6a52c75309a0targetblanka32dbeda"> <span class="glyphicon glyphicon-ok text-success" aria-hidden="true"></span> <strong>Repository version:</strong> <a href="https://gitlab.com/rscharpf/ipmn-timing/tree/a32dbed797e83eb45f85a7ac2bad6a52c75309a0" target="_blank">a32dbed</a> </a>
</p>
</div>
<div id="strongRepositoryversionstrongahrefhttpsgitlabcomrscharpfipmntimingtreea32dbed797e83eb45f85a7ac2bad6a52c75309a0targetblanka32dbeda" class="panel-collapse collapse">
<div class="panel-body">
<p>
Great! You are using Git for version control. Tracking code development and connecting the code version to the results is critical for reproducibility. The version displayed above was the version of the Git repository at the time these results were generated. <br><br> Note that you need to be careful to ensure that all relevant files for the analysis have been committed to Git prior to generating the results (you can use <code>wflow_publish</code> or <code>wflow_git_commit</code>). workflowr only checks the R Markdown file, but you know if there are other scripts or data files that it depends on. Below is the status of the Git repository when the results were generated:
</p>
<pre><code>
Ignored files:
    Ignored:    analysis/.Rhistory
    Ignored:    code/clock.R
    Ignored:    data/~$Timing_Metrics.xlsx
    Ignored:    publish.R

</code></pre>
<p>
Note that any generated files, e.g. HTML, png, CSS, etc., are not included in this status report because it is ok for generated content to have uncommitted changes.
</p>
</div>
</div>
</div>
</div>
<hr>
</div>
<div id="versions" class="tab-pane fade">

<p>
These are the previous versions of the R Markdown and HTML files. If you’ve configured a remote Git repository (see <code>?wflow_git_remote</code>), click on the hyperlinks in the table below to view them.
</p>
<div class="table-responsive">
<table class="table table-condensed table-hover">
<thead>
<tr>
<th>
File
</th>
<th>
Version
</th>
<th>
Author
</th>
<th>
Date
</th>
<th>
Message
</th>
</tr>
</thead>
<tbody>
<tr>
<td>
html
</td>
<td>
<a href="https://glcdn.githack.com/rscharpf/ipmn-timing/raw/a32dbed797e83eb45f85a7ac2bad6a52c75309a0/public/timing.html" target="_blank">a32dbed</a>
</td>
<td>
rscharpf
</td>
<td>
2019-08-28
</td>
<td>
Host with GitLab.
</td>
</tr>
<tr>
<td>
Rmd
</td>
<td>
<a href="https://gitlab.com/rscharpf/ipmn-timing/blob/6c6d7cb3c196fa5ca4d36d0f522cd4ae0b7a41b7/analysis/timing.Rmd" target="_blank">6c6d7cb</a>
</td>
<td>
rscharpf
</td>
<td>
2019-08-28
</td>
<td>
add code/clock.jag. fiddled with original timing analysis
</td>
</tr>
<tr>
<td>
Rmd
</td>
<td>
<a href="https://gitlab.com/rscharpf/ipmn-timing/blob/d3d0879257f632272c936c25e76679a9ba91f19d/analysis/timing.Rmd" target="_blank">d3d0879</a>
</td>
<td>
rscharpf
</td>
<td>
2019-07-31
</td>
<td>
updated prior model 3
</td>
</tr>
<tr>
<td>
Rmd
</td>
<td>
<a href="https://gitlab.com/rscharpf/ipmn-timing/blob/9ff8961b4a348925cf43bc353f6d0c946e6e7f00/analysis/timing.Rmd" target="_blank">9ff8961</a>
</td>
<td>
rscharpf
</td>
<td>
2019-07-29
</td>
<td>
fix facet labels
</td>
</tr>
<tr>
<td>
Rmd
</td>
<td>
<a href="https://gitlab.com/rscharpf/ipmn-timing/blob/0b0a7c62bd560cc738b86d533dcd13c3d0b4a716/analysis/timing.Rmd" target="_blank">0b0a7c6</a>
</td>
<td>
rscharpf
</td>
<td>
2019-07-29
</td>
<td>
Start HG IPMN timing project
</td>
</tr>
</tbody>
</table>
</div>
<hr>
</div>
</div>
</div>
<p>We assume that the high-grade IPMN and the PDAC have a common ancestor, as indicated by the multiple passenger mutations shared by these cell populations. Let <span class="math inline">\(y_i\)</span> denote the number of mutations acquired in sample <span class="math inline">\(i\)</span> between the HG IPMN and the PDAC. Of interest is the number of years <span class="math inline">\(T_i\)</span> during which these mutations were acquired. Our model</p>
<p><span class="math display">\[
\begin{align}
y | T, \mu &amp;\sim \text{Poisson}\left(\mu T)\right)\\
\mu T &amp;\sim \text{Gamma}(a, b).
\end{align}
\]</span></p>
<p>While both <span class="math inline">\(\mu\)</span> and <span class="math inline">\(T\)</span> are unknown, we assume that the number of passenger mutation among patients in a previous study where HG IPMNs were discovered incidentally would be similar on average to the number of passenger mutations we identified in HG lesions.</p>
<pre class="r"><code>library(ggplot2)
library(tidyverse)
library(readxl)
library(magrittr)
unixFriendly &lt;- function(x){
  x &lt;- colnames(x)
  x &lt;- tolower(x)
  x &lt;- gsub(&quot; &quot;, &quot;_&quot;, x)
  x
}
dat &lt;- read_excel(file.path(&quot;data&quot;,
                            &quot;Timing_Metrics.xlsx&quot;),
                  sheet=2) %&gt;%
  &quot;[&quot;(-nrow(.), ) %&gt;%
  set_colnames(unixFriendly(.)) %&gt;%
  mutate(additional=mutations_ca-mutations_hg_ipmn)
avgN &lt;- dat %&gt;%
  summarize(mean=mean(additional)) %&gt;%
  &quot;[[&quot;(1)</code></pre>
<pre class="r"><code>ipmn.age &lt;- read_excel(file.path(&quot;data&quot;,
                                 &quot;Timing_Metrics.xlsx&quot;),
                       sheet=1) %&gt;%
  &quot;[&quot;(1:2, ) %&gt;%
  set_colnames(c(&quot;age&quot;, &quot;n&quot;, &quot;url&quot;)) %&gt;%
  mutate(age=as.numeric(age),
         n=as.integer(n))
pdac.age &lt;- read_excel(file.path(&quot;data&quot;,
                                 &quot;Timing_Metrics.xlsx&quot;),
                       sheet=1) %&gt;%
  &quot;[&quot;(5:6, ) %&gt;%
  set_colnames(c(&quot;age&quot;, &quot;n&quot;, &quot;url&quot;)) %&gt;%
  mutate(age=as.numeric(age),
         n=as.integer(n))
Tavg &lt;- sum((pdac.age$age * pdac.age$n))/sum(pdac.age$n) -
  sum(ipmn.age$age * ipmn.age$n)/sum(ipmn.age$n)
round(Tavg, 2)</code></pre>
<pre><code>[1] 2.46</code></pre>
<pre class="r"><code>mu &lt;- avgN/Tavg ## mutations per year</code></pre>
<p>These calculations suggest 15.5 passenger mutations accumulate during the HG-&gt;PDAC transition at an average rate of 6.3 mutations per year. The difference in the average age of patients diagnosed with PDAC and the average age of patients with HG IPMNs discovered incidentally is 2.5. Taken together, we entertain priors for the number of mutations <span class="math inline">\(\mu T\)</span> during the HG-&gt;PDAC transition by loosely centering a gamma distribution at 15.5 and assuming that the rate given by <span class="math inline">\(\mu\)</span> is known.</p>
<div id="prior-model-1-middle" class="section level2">
<h2>Prior model 1 (middle)</h2>
<p>The shape parameter <span class="math inline">\(a\)</span> can be interpreted as the sum of counts from b prior observations. Below, we assume 7.75 mutations in 0.5 observations. This prior is centered at 15.5 mutations.</p>
<pre class="r"><code>## mu*Tavg ## 15.5
##posterior is also gamma.  center mean at a/b=15.5
aprime &lt;- a + dat$additional
bprime &lt;- b + 1  ## one sample
posterior &lt;- matrix(NA, 1000, nrow(dat))
for(j in seq_along(aprime)){
  posterior[, j] &lt;- rgamma(1000, aprime[j], rate=bprime)
}</code></pre>
<p>Since the posterior is for <span class="math inline">\(\mu T\)</span> where <span class="math inline">\(\mu\)</span> assumed to be known, we scale the posterior by <span class="math inline">\(\mu\)</span> to transform the scale to years.</p>
<pre class="r"><code>avg.years &lt;- colMeans(posterior) / mu
lower &lt;- apply(posterior, 2, quantile, 0.025) / mu
upper &lt;- apply(posterior, 2, quantile, 0.975) / mu</code></pre>
</div>
<div id="prior-model-2" class="section level2">
<h2>Prior model 2</h2>
<p>For prior model 2, our prior is equivalent to observing 7.75 mutations from 1 observations. This prior is centered at 7.8 mutations. This prior model with a lower mean than prior model 1 implies a faster transition from HGD to PDAC than prior model 1, since posterior summaries for the number of years will reflect this shrinkage. This prior is centered at 7.8 mutations.</p>
<pre class="r"><code>## mu*Tavg ## 15.5
##posterior is also gamma.  center mean at a/b=15.5
aprime &lt;- a + dat$additional
bprime &lt;- b + 1  ## one sample
posterior &lt;- matrix(NA, 1000, nrow(dat))
for(j in seq_along(aprime)){
  posterior[, j] &lt;- rgamma(1000, aprime[j], rate=bprime)
}
avg.years2 &lt;- colMeans(posterior) / mu
lower2 &lt;- apply(posterior, 2, quantile, 0.025) / mu
upper2 &lt;- apply(posterior, 2, quantile, 0.975) / mu</code></pre>
</div>
<div id="prior-model-3-longest-ipmn-pdac" class="section level2">
<h2>Prior model 3 (longest IPMN-&gt;PDAC)</h2>
<p>For prior model 3, we assume 12 mutations in 0.25 observations. This prior is centered at 48 mutations and will shrink posterior summaries for the number of years to higher values than prior models 1 and 2.</p>
<pre class="r"><code>## mean = shape/rate
a &lt;- 23.9/2
b &lt;- 1/4
aprime &lt;- a + dat$additional
bprime &lt;- b + 1  ## one sample
posterior &lt;- matrix(NA, 1000, nrow(dat))
for(j in seq_along(aprime)){
  posterior[, j] &lt;- rgamma(1000, aprime[j], rate=bprime)
}
avg.years3 &lt;- colMeans(posterior) / mu
lower3 &lt;- apply(posterior, 2, quantile, 0.025) / mu
upper3 &lt;- apply(posterior, 2, quantile, 0.975) / mu</code></pre>
</div>
<div id="old-text" class="section level1">
<h1>old text</h1>
<p>We use the average age for only the small study with sporadic cases and a weighted average of the ages for two previous studies with PDACs corresponding to 62 and 69.6 years, respectively. Setting <span class="math inline">\(T\)</span> to 69.6-62, or 7.6, and assuming that the true mutation rate is half of that derived above, we center our prior at 23.9 by using a gamma prior with shape and rate parameters 11.95 and 0.25).</p>
<div id="summary" class="section level2">
<h2>Summary</h2>
<pre class="r"><code>priormodels &lt;- rep(paste0(&quot;prior_model_&quot;, 1:3), each=nrow(dat)) %&gt;%
  factor(., levels=unique(.))
facetlabels &lt;- gsub(&quot;p&quot;, &quot;P&quot;, levels(priormodels)) %&gt;%
  gsub(&quot;_&quot;, &quot; &quot;, .)
facetlabels &lt;- setNames(facetlabels, levels(priormodels))
dat$case &lt;- factor(dat$case, levels=rev(dat$case))
tab &lt;- tibble(id=rep(dat$case, 3),
              years=c(avg.years, avg.years2, avg.years3),
              lower=c(lower, lower2, lower3),
              upper=c(upper, upper2, upper3)) %&gt;%
  mutate(id=factor(id, levels=rev(dat$case))) %&gt;%
  mutate(a=rep(7.75, nrow(.)),
         b=rep(c(0.5, 1, 0.025), each=nrow(dat)),
         prior_model=priormodels) %&gt;%
  left_join(dat, by=c(&quot;id&quot;=&quot;case&quot;))
ggplot(tab, aes(ymin=lower, ymax=upper, x=id, xend=id)) +
  geom_point(aes(y=years), shape=21, color=&quot;gray&quot;) +
  geom_errorbar(width=0.2) +
  coord_flip() +
  facet_wrap(~prior_model, labeller=labeller(prior_model=facetlabels)) +
  theme(panel.background=element_rect(fill=&quot;white&quot;, color=&quot;black&quot;)) +
  xlab(&quot;&quot;) + ylab(&quot;Years&quot;)</code></pre>
<p><img src="figure/timing.Rmd/tab-1.png" width="672" style="display: block; margin: auto;" /></p>
<p>
<button type="button" class="btn btn-default btn-xs btn-workflowr btn-workflowr-fig" data-toggle="collapse" data-target="#fig-tab-1">
Past versions of tab-1.png
</button>
</p>
<div id="fig-tab-1" class="collapse">
<div class="table-responsive">
<table class="table table-condensed table-hover">
<thead>
<tr>
<th>
Version
</th>
<th>
Author
</th>
<th>
Date
</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<a href="https://gitlab.com/rscharpf/ipmn-timing/blob/a32dbed797e83eb45f85a7ac2bad6a52c75309a0/public/figure/timing.Rmd/tab-1.png" target="_blank">a32dbed</a>
</td>
<td>
rscharpf
</td>
<td>
2019-08-28
</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<div id="averages" class="section level2">
<h2>Averages</h2>
<pre class="r"><code>targeted &lt;- tibble(rev(levels(tab$id)),
                   captured=c(1,1,1,0,1,
                              0,0,0,1,0,
                              0,0,0,1,0,
                              1,0,0)) %&gt;%
  set_colnames(c(&quot;id&quot;, &quot;captured&quot;)) %&gt;%
  mutate(id=factor(id, levels=levels(tab$id)))
tab2 &lt;- left_join(tab, targeted, by=&quot;id&quot;)
tab2 %&gt;% group_by(prior_model) %&gt;%
  summarize(average=mean(years),
            lower=mean(lower),
            upper=mean(upper))</code></pre>
<pre><code># A tibble: 3 x 4
  prior_model   average lower upper
  &lt;fct&gt;           &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt;
1 prior_model_1    2.46  1.60  3.52
2 prior_model_2    1.85  1.22  2.63
3 prior_model_3    3.49  2.35  4.87</code></pre>
<pre class="r"><code>tab2 %&gt;% group_by(prior_model, captured) %&gt;%
  summarize(average=mean(years),
            lower=mean(lower),
            upper=mean(upper)) %&gt;%
  arrange(captured)</code></pre>
<pre><code># A tibble: 6 x 5
# Groups:   prior_model [3]
  prior_model   captured average lower upper
  &lt;fct&gt;            &lt;dbl&gt;   &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt;
1 prior_model_1        0    2.61  1.74  3.68
2 prior_model_2        0    1.96  1.31  2.78
3 prior_model_3        0    3.66  2.50  5.06
4 prior_model_1        1    2.22  1.39  3.26
5 prior_model_2        1    1.66  1.07  2.40
6 prior_model_3        1    3.22  2.12  4.56</code></pre>
<br>
<p>
<button type="button" class="btn btn-default btn-workflowr btn-workflowr-sessioninfo" data-toggle="collapse" data-target="#workflowr-sessioninfo" style="display: block;">
<span class="glyphicon glyphicon-wrench" aria-hidden="true"></span> Session information
</button>
</p>
<div id="workflowr-sessioninfo" class="collapse">
<pre class="r"><code>sessionInfo()</code></pre>
<pre><code>R version 3.6.0 (2019-04-26)
Platform: x86_64-apple-darwin18.5.0/x86_64 (64-bit)
Running under: macOS Mojave 10.14.6

Matrix products: default
BLAS:   /Users/rscharpf/Rversions/R-3.6.0/lib/x86_64/libRblas.dylib
LAPACK: /Users/rscharpf/Rversions/R-3.6.0/lib/x86_64/libRlapack.dylib

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] magrittr_1.5    readxl_1.3.1    forcats_0.4.0   stringr_1.4.0  
 [5] dplyr_0.8.3     purrr_0.3.2     readr_1.3.1     tidyr_0.8.3    
 [9] tibble_2.1.3    tidyverse_1.2.1 ggplot2_3.2.1  

loaded via a namespace (and not attached):
 [1] tidyselect_0.2.5     xfun_0.9             haven_2.1.1         
 [4] lattice_0.20-38      colorspace_1.4-1     generics_0.0.2      
 [7] vctrs_0.2.0          htmltools_0.3.6      yaml_2.2.0          
[10] utf8_1.1.4           rlang_0.4.0          pillar_1.4.2        
[13] glue_1.3.1           withr_2.1.2          modelr_0.1.5        
[16] munsell_0.5.0        gtable_0.3.0         workflowr_1.4.0.9000
[19] cellranger_1.1.0     rvest_0.3.4          evaluate_0.14       
[22] labeling_0.3         knitr_1.24           fansi_0.4.0         
[25] broom_0.5.2          Rcpp_1.0.2           scales_1.0.0        
[28] backports_1.1.4      jsonlite_1.6         fs_1.3.1            
[31] hms_0.5.1            digest_0.6.20        stringi_1.4.3       
[34] grid_3.6.0           rprojroot_1.3-2      cli_1.1.0           
[37] tools_3.6.0          lazyeval_0.2.2       crayon_1.3.4        
[40] whisker_0.3-2        pkgconfig_2.0.2      zeallot_0.1.0       
[43] xml2_1.2.2           lubridate_1.7.4      assertthat_0.2.1    
[46] rmarkdown_1.15       httr_1.4.1           rstudioapi_0.10     
[49] R6_2.4.0             nlme_3.1-141         git2r_0.26.1        
[52] compiler_3.6.0      </code></pre>
</div>
</div>
</div>


<!-- Adjust MathJax settings so that all math formulae are shown using
TeX fonts only; see
http://docs.mathjax.org/en/latest/configuration.html.  This will make
the presentation more consistent at the cost of the webpage sometimes
taking slightly longer to load. Note that this only works because the
footer is added to webpages before the MathJax javascript. -->
<script type="text/x-mathjax-config">
  MathJax.Hub.Config({
    "HTML-CSS": { availableFonts: ["TeX"] }
  });
</script>



</div>

<script>

// add bootstrap table styles to pandoc tables
function bootstrapStylePandocTables() {
  $('tr.header').parent('thead').parent('table').addClass('table table-condensed');
}
$(document).ready(function () {
  bootstrapStylePandocTables();
});


</script>

<!-- tabsets -->

<script>
$(document).ready(function () {
  window.buildTabsets("TOC");
});

$(document).ready(function () {
  $('.tabset-dropdown > .nav-tabs > li').click(function () {
    $(this).parent().toggleClass('nav-tabs-open')
  });
});
</script>

<!-- code folding -->


<!-- dynamically load mathjax for compatibility with self-contained -->
<script>
  (function () {
    var script = document.createElement("script");
    script.type = "text/javascript";
    script.src  = "https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML";
    document.getElementsByTagName("head")[0].appendChild(script);
  })();
</script>

</body>
</html>
